package de.hhn.seb.pmt.mtb.prototype;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.swing.JFileChooser;
import javax.swing.JList;


public class Export {

	public static void toXML(JList list) {

		if (list == null) {
			return;
		}

		try {
			JFileChooser jfc = new JFileChooser();
			File xmlFile = new File("");
			int returnVal = jfc.showOpenDialog(null);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				xmlFile = jfc.getSelectedFile();
			}

			FileOutputStream xml = new FileOutputStream(xmlFile);

			OutputStream os = new BufferedOutputStream(xml);
			PrintWriter out = new PrintWriter(os);

			out.println("<?xml version='1.0' encoding='UTF-8'?>");
			// XML-Dokument
			out.print("<Strecke ");
			out.print("name ='" + "Ort" + "' ");
			out.print("land ='" + "z.b: Deutschland" + "' ");
			out.println(" >");

			for (int i = 0; i < list.getModel().getSize(); i++) {
				Object element = list.getModel().getElementAt(i);
				if (element instanceof StreckenName
						&& !((StreckenName) element).toString().trim()
								.equals("")) {
					out.print("<");
					out.print((StreckenName) element);
					out.println(" />");
				}
				if (element instanceof Irgendwas
						&& !((Irgendwas) element).toString().trim().equals("")) {
					out.print("<");
					out.print((Irgendwas) element);
					out.println(" />");
				}
				if (element instanceof KrankenHaus
						&& !((KrankenHaus) element).toString().trim()
								.equals("")) {
					out.print("<");
					out.print((KrankenHaus) element);
					out.println(" />");
				}
			}

			// Abschluss des Dokuments durch Schlie�en.
			out.println("</Strecke>");

			out.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
